//
//  AttentionFriendController.m
//  Arts1
//
//  Created by apple on 15/9/6.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "AttentionFriendController.h"
#import "AllHeader.h"
#import "AttentionCollectionViewCell.h"
#import "AttentionFriendModel.h"

//#define kURL @"http://arts1.dlab.com.cn/app/setfollow"
#define kURL @"http://arts1.cn/app/setfollow"
#define kMyFollowersURL @"http://arts1.cn/app/myfriend"


@interface AttentionFriendController ()<UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong) NSMutableArray *friendArray;

@end

@implementation AttentionFriendController

static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)friendArray
{
    if (_friendArray == nil)
    {
        self.friendArray = [NSMutableArray array];
    }
    return _friendArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = kBackgroundColor;

    [self.collectionView registerClass:[AttentionCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.alwaysBounceVertical = YES;
    [self.collectionView setContentInset:(UIEdgeInsetsMake(6, 0, 49, 0))];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(reLoadView) name:@"reLoadFriendView" object:nil];
    
    NSNotificationCenter *centerDelFriend = [NSNotificationCenter defaultCenter];
    [centerDelFriend addObserver:self selector:@selector(centerDelFriend:) name:@"delFriend" object:nil];

}

/**
 *  取消关注
 *
 *  @param centerDelFriend 接收关注的参数
 */
- (void)centerDelFriend:(NSNotification *)centerDelFriend
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    
    NSArray *array = centerDelFriend.object;
    
    NSDictionary *pardic = nil;
    if (uid && key && array[1]) {
        pardic = @{@"type":@"user",
                   @"uid":uid,
                   @"key":key,
                   @"act":array[0],
                   @"userId":array[1]};
    }
    
    [manage GET:kURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            
//            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//            NSNotification *cation = [[NSNotification alloc] initWithName:@"reLoadFriendView" object:nil userInfo:nil];
//            [center postNotification:cation];
            
            [self.collectionView reloadData];
            // 请求成功的时候调用这个block
            NSLog(@"数据请求-------------------%@", dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
}

- (void)reLoadView
{
    //数据请求
    [self requestData];
    [self.collectionView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    //数据请求
    [self requestData];
    [self.collectionView reloadData];
}

//请求数据
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = [NSDictionary dictionary];
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }
    
    [manage POST:kMyFollowersURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             int status = [dic[@"status"] intValue];
             if (status == 1) {
                 
                 NSArray *list = dic[@"list"];
                 [self.friendArray removeAllObjects];
                 for (NSDictionary *dict in list)
                 {
                     AttentionFriendModel *model = [[AttentionFriendModel alloc] init];
                     [model setValuesForKeysWithDictionary:dict];
                     [self.friendArray addObject:model];
                 }
             }
             //刷新数据
             [self.collectionView reloadData];
             // 请求成功的时候调用这个block
             NSLog(@"我的好友列表数据请求--%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.friendArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AttentionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    AttentionFriendModel *model = _friendArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH - 20, 60 * HEIGHT);
    
}

@end
